package com.smec.remes.module.submit.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutSubmitDetailsBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.bean.MatnrList;
import com.smec.remes.bean.SubmitDetails;
import com.smec.remes.bean.SubmitDetailsConfirme;
import com.smec.remes.module.submit.SmecSubmitPresenter;
import com.smec.remes.module.submit.adapter.SmecWuLiaoAdapter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.FullyLinearLayoutManager;
import com.smec.remes.util.WmsConstant;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/20.
 */

public class SmecSubmitDetailsActivity extends SmecBaseActivity<SmecSubmitPresenter> {

    private LayoutSubmitDetailsBinding layoutSubmitDetailsBinding;
    private SmecWuLiaoAdapter smecWuLiaoAdapter;
    public static final String DETAILS="DETAILS";
    public static final String DETAILSFAIL="DETAILSFAIL";
    public static final String Refresh="Refresh";
    private boolean mantr=true;
    private String name,use,date,applyOrderNo,node,remark,dbName;
    private String _fragment;
    private Handler mHandler=new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layoutSubmitDetailsBinding=DataBindingUtil.setContentView(this, R.layout.layout_submit_details);
        FullyLinearLayoutManager managerMatnrs = new FullyLinearLayoutManager(this);
        smecWuLiaoAdapter=new SmecWuLiaoAdapter(this);
        layoutSubmitDetailsBinding.mantrRecyclerView.setLayoutManager(managerMatnrs);
        layoutSubmitDetailsBinding.mantrRecyclerView.setNestedScrollingEnabled(false);
        layoutSubmitDetailsBinding.mantrRecyclerView.setAdapter(smecWuLiaoAdapter);
        _return();
        _hide();
        _getIntent();
        _btnOnclick();
        _refresh();
    }

    public void _return(){
        layoutSubmitDetailsBinding.contractTopbar.getTvTextLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecSubmitDetailsActivity.this.finish();
                if(_fragment.equals("unfinish")){
                    mRxBus.post("unfinish","unfinish");
                }else {
                    mRxBus.post("finish","finish");
                }
            }
        });
    }
    public void _btnOnclick(){
        layoutSubmitDetailsBinding.llWeiOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(SmecSubmitDetailsActivity.this,SubmitWeiShouHuoActivity.class);
                intent.putExtra("applyOrderNo",applyOrderNo);
                startActivity(intent);
            }
        });
        layoutSubmitDetailsBinding.llShouTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(SmecSubmitDetailsActivity.this,SubmitShouHuoActivity.class);
                intent.putExtra("applyOrderNo",applyOrderNo);
                startActivity(intent);
            }
        });

        layoutSubmitDetailsBinding.backRe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SmecSubmitDetailsActivity.this,SmecBackActivity.class);
                intent.putExtra("applyOrderNo",applyOrderNo);
                startActivity(intent);
            }
        });
    }

    public void _getIntent(){
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        use=intent.getStringExtra("use");
        date=intent.getStringExtra("date");
        applyOrderNo=intent.getStringExtra("applyOrderNo");
        _fragment=intent.getStringExtra("_fragment");
        node=intent.getStringExtra("node");
        remark=intent.getStringExtra("remark");
        dbName=intent.getStringExtra("dbName");
        layoutSubmitDetailsBinding.tvName.setText(name);
        layoutSubmitDetailsBinding.tvUse.setText(use);
        layoutSubmitDetailsBinding.tvDate.setText(date);
        layoutSubmitDetailsBinding.tvNode.setText(node);
        layoutSubmitDetailsBinding.remark.setText(remark);
        if(node.equals("维保经理审批中")|| node.equals("维保站长校对中")){
            layoutSubmitDetailsBinding.llNode.setBackground(this.getResources().getDrawable(R.drawable.status_weibao));
            layoutSubmitDetailsBinding.llWeiOne.setVisibility(View.GONE);
            layoutSubmitDetailsBinding.llShouTwo.setVisibility(View.GONE);
            layoutSubmitDetailsBinding.backRe.setVisibility(View.GONE);
        }else if(node.equals("待分配")){
            layoutSubmitDetailsBinding.llNode.setBackground(this.getResources().getDrawable(R.drawable.status_daifen));
        }else if(node.equals("待收货")){
            layoutSubmitDetailsBinding.llNode.setBackground(this.getResources().getDrawable(R.drawable.status_daishou));
        }else if(node.equals("已完成")){
            layoutSubmitDetailsBinding.llNode.setBackground(this.getResources().getDrawable(R.drawable.status_wancheng));
        }else if(node.equals("调拨单待出库")){
            layoutSubmitDetailsBinding.llNode.setBackground(this.getResources().getDrawable(R.drawable.status_db));
            if(dbName!=null && !dbName.equals("")){
                layoutSubmitDetailsBinding.smecDb.setVisibility(View.VISIBLE);
                layoutSubmitDetailsBinding.dbName.setText(dbName);
            }else {
                layoutSubmitDetailsBinding.smecDb.setVisibility(View.GONE);
            }
        }else {
            layoutSubmitDetailsBinding.llNode.setBackground(this.getResources().getDrawable(R.drawable.status_bohui));
        }
    }

    public void _refresh(){
        layoutSubmitDetailsBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        layoutSubmitDetailsBinding.ptr.setBottomView(new LoadingView(getApplicationContext()));
        layoutSubmitDetailsBinding.ptr.setAutoLoadMore(true);
        layoutSubmitDetailsBinding.ptr.setEnableOverScroll(false);
        layoutSubmitDetailsBinding.ptr.setEnableLoadmore(false);    //设置是否关闭上拉加载
        layoutSubmitDetailsBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                mPresenter._submitDetails(applyOrderNo);
            }
        });
        layoutSubmitDetailsBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                layoutSubmitDetailsBinding.ptr.startRefresh();   //让其自动下拉
            }
        },500);

    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(DETAILS)
            }
    )

    public void _detailsSuccess(ArrayList<SubmitDetails> arrayList){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                layoutSubmitDetailsBinding.ptr.finishRefreshing();   //下拉刷新关闭
            }

        },200);
        if(CommonUtils.notEmpty(arrayList)){
            ArrayList<MatnrList> matnrListArrayList=arrayList.get(0).getMatnrList();
            smecWuLiaoAdapter.setArrayList(matnrListArrayList);
            ArrayList<SubmitDetailsConfirme> list=arrayList.get(0).getConfirmedList();//已收货
            layoutSubmitDetailsBinding.tvShouNum.setText(String.valueOf(_getNum(list)));
            ArrayList<SubmitDetailsConfirme> tuihuiArrayList=arrayList.get(0).getCanceledList();//已退回
            if(CommonUtils.notEmpty(tuihuiArrayList)){
                layoutSubmitDetailsBinding.backNum.setText(String.valueOf(_getNum(tuihuiArrayList)));
            }else {
                layoutSubmitDetailsBinding.backNum.setText("0");
            }
            ArrayList<SubmitDetailsConfirme> submitDetailsConfirmeArrayList=arrayList.get(0).getTobeconfirmList();//待收货
            layoutSubmitDetailsBinding.tvWeiNum.setText(String.valueOf(_getNum(submitDetailsConfirmeArrayList)));
        }
    }

    public int _getNum(ArrayList<SubmitDetailsConfirme> list){
        int shouNum=0;
        if(CommonUtils.notEmpty(list)){
            for(int i=0;i<list.size();i++){
                if(!TextUtils.isEmpty(list.get(i).getFlag())){
                    shouNum+=1;
                }
            }
        }
        return shouNum;
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(DETAILSFAIL)
            }
    )

    public void _detailsFail(String a){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                layoutSubmitDetailsBinding.ptr.finishRefreshing();   //下拉刷新关闭
            }

        },200);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(Refresh)
            }
    )

    public void _Refresh(String a){
        layoutSubmitDetailsBinding.ptr.startRefresh();
    }

    @Override
    public SmecSubmitPresenter getPresenter() {
        return new SmecSubmitPresenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        layoutSubmitDetailsBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter._submitDetails(applyOrderNo);
            }
        },400);
    }

    public void _hide(){
        layoutSubmitDetailsBinding.tvWuliao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mantr){
                    layoutSubmitDetailsBinding.mantrRecyclerView.setVisibility(View.GONE);
                    layoutSubmitDetailsBinding.tvWuliao.setImageResource(R.drawable.smec_remes_down);
                    mantr=false;
                }else {
                    layoutSubmitDetailsBinding.mantrRecyclerView.setVisibility(View.VISIBLE);
                    layoutSubmitDetailsBinding.tvWuliao.setImageResource(R.drawable.smec_remes_up);
                    mantr=true;
                }
            }
        });
    }
}
